#include <behaviortree_cpp_v3/behavior_tree.h>
#include <behaviortree_cpp_v3/bt_factory.h>
using namespace std;

class PrintBlackBoard : public BT::SyncActionNode
{
public:
    PrintBlackBoard(const string& name, const BT::NodeConfiguration& config)
        : BT::SyncActionNode(name, config)
    {
    }

    BT::NodeStatus tick() override
    {
        auto Name = getInput<string>("Name");
        int age;
        getInput<int>("age", age);
        if (!Name)
        {
            return BT::NodeStatus::FAILURE;
        }

        cout << this->name() << "- name:" << Name.value() << " age:" << age << endl;
        return BT::NodeStatus::SUCCESS;
    }

    static BT::PortsList providedPorts()
    {
        return {
            BT::InputPort<string>("Name"),
            BT::InputPort<int>("age")
        };
    }
};


/* SetBlackboard 设置黑板变量值 */
static const char* xml_text = R"(

 <root main_tree_to_execute = "MainTree" >

     <BehaviorTree ID="MainTree">
        <Sequence name="root">
            <SetBlackboard value="lilisi" output_key="Name" />
            <SetBlackboard value="18" output_key="age" />
            <PrintBlackBoard Name="{Name}" age="{age}"/>
        </Sequence>
     </BehaviorTree>

 </root>
 )";

int main()
{
    BT::BehaviorTreeFactory factory;
    // 注册节点
    factory.registerNodeType<PrintBlackBoard>("PrintBlackBoard");

    auto tree = factory.createTreeFromText(xml_text);
    tree.tickRootWhileRunning();

    return 0;
}